home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mmdf / mmdf-IIb.43 / src / prog / pr_errmsg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-28  |  5.0 KB  |  271 lines

  1. /*
  2.  *    MULTI-CHANNEL MEMO DISTRIBUTION FACILITY  (MMDF)
  3.  *
  4.  *    Department of Electrical Engineering
  5.  *    University of Delaware
  6.  *    Newark, Delaware  19711
  7.  *
  8.  *
  9.  *    Program Channel (inbound): Pass message into MMDF
  10.  *
  11.  *
  12.  *    P R _ E R R M S G . C
  13.  *    =====================
  14.  *
  15.  *    send message to report error
  16.  *
  17.  *    J.B.D.Pardoe
  18.  *    University of Cambridge Computer Laboratory
  19.  *    October 1985
  20.  *    
  21.  *    July 86 - Added #ifdef V4_2BSD around <sys/file.h>    
  22.  *        ECB    
  23.  */
  24.  
  25.  
  26. #include <stdio.h>
  27. #include "util.h"
  28. #include "mmdf.h"
  29. #ifdef SYS5r3
  30. #include <fcntl.h>
  31. #endif
  32. #ifdef V4_2BSD
  33. #include <sys/file.h>
  34. #else
  35. #include <fcntl.h>
  36. #endif /* V4_2BSD */
  37.  
  38.  
  39. #define EX_OK    0 /* everything successful */
  40. #define EX_ADDR    1 /* bad addresses */
  41. #define EX_MECH    2 /* interaction with MMDF failed */
  42.  
  43. #define PR_COMM '#'
  44. #define PR_ERR  '*'
  45.  
  46.  
  47. FILE *errmsg_file;
  48. long msg_start;
  49.  
  50. extern char *sender;
  51. extern char *sitesignature, *mmdflogin, *supportaddr, *locfullname;
  52. extern errno, prog_debug;
  53.  
  54. static mail_support();
  55.  
  56. static char sender_buf[256];
  57.  
  58.  
  59. errmsg_open () 
  60. {
  61.     extern long getpid ();
  62.     char buf[80]; 
  63.     
  64.     rewindable_msg ();
  65.  
  66.     if (!sender) {
  67.     /* find a return address: order of preference    */
  68.     /* is Resent_From > Sender > From        */
  69.  
  70.     int prio = 0;
  71.     long msg_pos = ftell (stdin);
  72.  
  73.     rewind_msg ();
  74.  
  75.     while (gets (buf) != NULL && buf[0] != '\0') {
  76.         int newprio; register char *p;
  77.  
  78.         if (lexnequ (buf, "From:", 5)) {
  79.         p = &buf[5]; newprio = 1;
  80.         } else if (lexnequ (buf, "Sender:", 7)) {
  81.         p = &buf[7]; newprio = 2;
  82.         } else if (lexnequ (buf, "Resent-From:", 12)) {
  83.         p = &buf[12]; newprio = 3;
  84.         } else {
  85.         newprio = 0;
  86.         }
  87.         
  88.         if (newprio > prio) {
  89.         while (*p == ' ' || *p == '\t') p++;
  90.         strcpy (sender_buf, p);
  91.         sender = sender_buf;
  92.         prio = newprio;
  93.         }
  94.     }
  95.     fseek (stdin, msg_pos, 0);
  96.     }
  97.  
  98.     sprintf (buf, "/tmp/rcvprg.eXXXXXX");
  99.     mktemp(buf);
  100.     errmsg_file = fopen (buf, "w+");
  101.     if (errmsg_file == NULL) {
  102.     printf ("%ccouldn't open errmsg\n", PR_ERR);
  103.     exit (EX_MECH);
  104.     }
  105.     unlink (buf);
  106. }
  107.  
  108.  
  109. errmsg_send ()
  110. {
  111.     int rc;
  112. #ifdef DEBUG
  113.     if (prog_debug) {
  114.     printf ("%csending error message to %s\n", PR_COMM,
  115.                 sender?sender:"MMDF support");
  116.     }
  117. #endif
  118.  
  119.     if (!sender) {
  120.     mail_support ("(unknown sender)");
  121.     return;
  122.     }    
  123.  
  124.     mopen (sender, "Failed Mail");
  125.     mprintf ("It was not possible to fully deliver ");
  126.     mprintf ("your message\n(included below) for the ");
  127.     mprintf ("reason(s) given below\n\n");
  128.     minclude_errmsg ();
  129.     rc = mclose ();
  130.  
  131.     if (rc != 0) {
  132. #ifdef DEBUG
  133.     if (prog_debug)
  134.         printf ("%cfailed: trying MMDF support\n", PR_ERR);
  135. #endif /* DEBUG */
  136.     mail_support (sender);
  137.     }
  138. }
  139.  
  140.  
  141. static mail_support (sender)
  142.     char *sender;
  143. {
  144.     mopen (mmdflogin, "Unreturned Failed Mail");
  145.     mprintf ("Unable to return message to %s\n\n", sender);
  146.     minclude_errmsg ();
  147.     if (mclose () != 0) {
  148.     printf ("%cCouldn't mail %s about mail from %s\n",
  149.         PR_ERR, supportaddr, sender);
  150.     exit (EX_MECH);
  151.     }
  152. }
  153.  
  154.  
  155. rewindable_msg ()
  156. {
  157.     long getpid ();
  158.     char buf[1024];
  159.     int f, n;
  160.  
  161.     if (fseek (stdin, msg_start, 0) != -1) return; /* rewindable! */
  162.  
  163.     sprintf (buf, "/tmp/rcvprg.mXXXXXX");
  164.     mktemp(buf);
  165.     if ((f = open (buf, O_RDWR|O_CREAT|O_EXCL, 0640)) < 0) {
  166.     printf ("%cfailed to open message file %s\n", PR_ERR, buf);
  167.     exit (EX_MECH);
  168.     }
  169.     unlink (buf);
  170.  
  171.     while ((n = read (0, buf, 1024)) > 0) {
  172.     if ((n = write (f, buf, n)) < 0) break;
  173.     }
  174.     if (n < 0) {
  175.     printf ("%ci/o error on message\n", PR_ERR);
  176.     exit (EX_MECH);
  177.     }
  178.     close(0);
  179.     dup(f);
  180.     close(f);
  181. }
  182.  
  183.  
  184. rewind_msg ()
  185. {
  186.     if (fseek (stdin, msg_start, 0) < 0) {
  187.     printf ("%cfailed to rewind message: %d\n", PR_ERR, errno);
  188.     exit (EX_MECH);
  189.     }
  190.     return (OK);
  191. }        
  192.  
  193.  
  194. lexnequ (str1, str2, n)
  195.     register char *str1, *str2;
  196. {
  197.     extern char chrcnv[];
  198.     while (chrcnv[*str1] == chrcnv[*str2++])
  199.     if (--n == 0 || *str1++ == 0) return (TRUE);
  200.     return (FALSE);
  201. }
  202.  
  203.  
  204.  
  205. /*  *
  206.  * Simple Message Sending
  207.  */
  208.  
  209. static msuccess;
  210.  
  211. mopen (to, subj)
  212.     char *to, *subj;
  213. {
  214.     char buff[64];
  215.     sprintf (buff, "%s <%s@%s>", sitesignature, mmdflogin, locfullname);
  216.     msuccess = (ml_1adr (NO, YES, buff, subj, to) == OK);
  217. }
  218.  
  219.  
  220. mprintf (f, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9)
  221.     char *f;
  222. {
  223.     char buff[128];
  224.     if (!msuccess) return;
  225.     sprintf (buff, f, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9);
  226.     msuccess = (ml_txt (buff) == OK);
  227. }
  228.  
  229.  
  230. minclude (f)
  231.     FILE *f;
  232. {
  233.     if (!msuccess) return;
  234.     msuccess = (ml_file (f) == OK);
  235. }
  236.  
  237.  
  238. mclose ()
  239. {
  240.     if (msuccess) {
  241.     msuccess = (ml_end (OK) == OK);
  242.     } else {
  243.     ml_end (NOTOK);
  244.     }
  245.     return (msuccess? 0 : -1);
  246. }
  247.  
  248.  
  249.  
  250. minclude_errmsg ()
  251. {
  252.     FILE *msg;
  253.     char *divider = 
  254.     "\n-------------------------------------------------------\n\n";
  255.  
  256.     /*
  257.      * rewind() doesn't return anything so I removed it from
  258.      * the msuccess statement. ECB
  259.     rewind (errmsg_file);
  260.      */
  261.     msuccess = (
  262.      fseek (errmsg_file,0L,0)    != -1   &&
  263.     ml_file (errmsg_file)        == OK   &&
  264.     ml_txt (divider)        == OK   &&
  265.     rewind_msg ()            != -1   &&
  266.     (msg = fdopen (0, "r"))        != NULL &&
  267.     ml_file (msg)             == OK   &&
  268.     ml_txt (divider)         == OK
  269.     );
  270. }
  271.